// pages/orderConfirm/orderConfirm.js
var utils = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderLines: [],
    totalPrice: 0,
    address: {
      name: '',
      telephone: '',
      detail: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var orderInfo = JSON.parse(options.orderInfo);
    this.setData({
      orderLines: orderInfo.orderLines,
      totalPrice: orderInfo.totalPrice
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data.address);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submitOrder() {
    var orderId = utils.generateOrderId();
    for (let orderLine of this.data.orderLines) {
      orderLine.orderNo = orderId;
    }
    var self = this;
    var totalFee=100;
    var msg='确认支付'+totalFee+'元吗?';
    wx.showModal({
      title: '支付确认',
      content: msg,
      success: function(res) {
        if (res.confirm) {
          let serverUrl = app.globalData.serverUrl + '/saveOrder';
          //这里调用微信支付，结算，并生成订单，保存到数据库中，后面进行订单状态的追踪
          wx.request({
            url: serverUrl,
            //这里也可以不用 json.stringify
            data: JSON.stringify({
              "orderNo": orderId,
              orderPrice: self.data.totalPrice,
              status: 1,
              receiveAddressId: 1,
              "orderLineList": self.data.orderLines
            }),
            method: 'POST',
            success: function(result) {
              //订单生成完成后，清理一下购物车
              self.processCars(self.data.orderLines);
              wx.showToast({
                title: '订单提交成功！',
                icon: 'success',
                duration: 2000
              });
              console.log(result)
            }
          })
        } else {
          console.log('取消支付')
        }
      }
    })
  },
  processCars(items) {
    console.log(items);
    var cars = wx.getStorageSync('car');
    for (var item of items) {
      utils.removeItemFromArr(cars, item, 'id')
    }
    wx.setStorageSync('car', cars);
  },
  wxpay: function() {
    var self = this;
    wx.login({
      success: function(res) {
        self.generateOrder(res.code);
      }
    })
  },
  generateOrder: function(code) {
    var self = this;
    let serverUrl = app.globalData.serverUrl + '/generateOrder';
    wx.request({
      url: serverUrl,
      method: 'POST',
      data: {
        total_fee: 20,
        body: '支付测试',
        attch: ''
      },
      success: function(res) {
        console.log(res);
        self.pay();
      }
    })
  },
  pay: function() {
    wx.requestPayment({
        timeStamp: '',
        nonceStr: '',
        package: '',
        signType: '',
        paySign: '',
        success: function() {
          wx.navigateBack({
            delta: 1,
            success: function(res) {
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 2000
              })
            }
          })
        }
      }

    )
  },
  selectReceiveAddress: function(e) {
    //跳转到详情页 
    //wx.redirectTo({
    wx.navigateTo({
      //在接收页面的url后面加上“？自定义名称=字符串”就可以通过url传值
      url: '../addresslist/addresslist',

    })
  }
})